package com.syni.mdd.yhd.common.all.service.impl.groupBuy;

import com.syni.mdd.yhd.common.all.dao.jpa.groupBuy.BmsDxGroupBuyRecordDao;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuy;
import com.syni.mdd.yhd.common.all.entity.groupBuy.BmsDxGroupBuyRecord;
import com.syni.mdd.yhd.common.all.service.groupBuy.BmsDxGroupBuyRecordService;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsDxGroupBuyRecordServiceImpl extends BaseService<BmsDxGroupBuyRecord,Integer> implements BmsDxGroupBuyRecordService {

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    BmsDxGroupBuyRecordDao bmsDxGroupBuyRecordDao;

    @Override
    public BmsDxGroupBuyRecordDao getBaseDao() {
        return bmsDxGroupBuyRecordDao;
    }

    @Override
    public void addGroupBuyRecord(BmsDxGroupBuy bmsDxGroupBuy,Integer operation) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("groupId",bmsDxGroupBuy.getId());
        conditionMap.put("operation",operation);
        conditionMap.put("agentId",AGENT_ID);
        List<BmsDxGroupBuyRecord> bmsDxGroupBuyRecords = bmsDxGroupBuyRecordDao.findAll(new SpecificationUtils(conditionMap));
        if(!(bmsDxGroupBuyRecords != null && bmsDxGroupBuyRecords.size() > 0)){
            BmsDxGroupBuyRecord bmsDxGroupBuyRecord = new BmsDxGroupBuyRecord();
            bmsDxGroupBuyRecord.setGroupId(bmsDxGroupBuy.getId());
            bmsDxGroupBuyRecord.setNewTime(new Date());
            bmsDxGroupBuyRecord.setOperation(operation);
            bmsDxGroupBuyRecord.setAgentId(AGENT_ID);
            bmsDxGroupBuyRecordDao.save(bmsDxGroupBuyRecord);
        }

    }
}
